package TestProject02.Test;

/*需求:
        把一个一维数组中的数据:0~15打乱顺序
        然后再按照4个一组的方式添加到二维数组当中
*/

import java.util.Random;

public class Test2 {
    public static void main(String[] args) {

        Random r = new Random();

        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        for (int i = 0; i < tempArr.length; i++) {
            int index = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }

        for (int i = 0; i < tempArr.length; i++) {
            System.out.print(tempArr[i] + " ");
        }

        System.out.println();
        System.out.println("----------------------------------------------");

        int[][] arr = new int[4][4];

        //给二维数组添加数据
        //解法一：
        //遍历一维数组tempArr得到每一个元素，把每一个元素一次添加到二维数组当中
        for (int i = 0; i < tempArr.length; i++) {
            arr[i / 4][i % 4] = tempArr[i];
        }

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("----------------------------------------------");

        //解法二：
        //遍历二维数组，给里面的每一个数据赋值
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = tempArr[index];
                index++;
            }
        }

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}
